package com.intelligent.ispc.utils;

import com.intelligent.ispc.external.rcall.common.LevelType;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * Created by julia on 16/4/10.
 */
public class ExportExcelUtils {
    /**
     * @Title
     * @Description=法
     * @author
     * @param workbook
     * @param sheetNum
     * @param sheetTitle
     * @param headers
     * @param result
     * @param out
     * @throws Exception
     */
    public void exportNDCExcel(HSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<String>> result,
                            Map<String, Object> searchParams, OutputStream out) throws Exception {
        HSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);
        sheet.setDefaultColumnWidth(30);
        sheet.setColumnWidth(0, 18*256);
        sheet.setColumnWidth(1, 45*256);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        style.setFont(font);

        style.setWrapText(true);

        HSSFCellStyle style1 = workbook.createCellStyle();
        style1.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        if (sheetNum == 0) {
            for (int i = 0; i < headers.length; i++) {
                HSSFRow row1 = sheet.createRow(i);
                if (i >9) {
                    HSSFCell cell0 = row1.createCell(0);
                    HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                    cell0.setCellValue(text.toString());
                } else {
                    HSSFCell cell = row1.createCell(0);
                    HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                    cell.setCellValue(text.toString());
                }

            }

            if (result != null) {
                int index = 0;
                for (List<String> m : result) {
                    int cellIndex = 1;
                    for (String str : m) {
                        HSSFRow row = sheet.getRow(index);

                        if (index > 27) {
                            HSSFCell cell1 = row.createCell(cellIndex);
                            HSSFCellStyle style2 = workbook.createCellStyle();
                            HSSFFont contentFont = workbook.createFont();
                            contentFont.setFontHeightInPoints((short) 12);
                            if (!str.equals("-")) {
                                String[] color = str == null ? new String[]{"", ""} : str.split(",");
                                cell1.setCellValue(color[0]);
                                contentFont.setColor(setColor(color[1]));
                                style2.setFont(contentFont);
                            } else {
                                cell1.setCellValue(str);
                            }
                            cell1.setCellStyle(style2);


                        } else {
                            HSSFCell cell1 = row.createCell(cellIndex);
                            cell1.setCellValue(str == null ? "" : str);
                        }
                        index++;


                    }
                }
            }

            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

            BufferedImage bufferImg = ImageIO.read(new File(searchParams.get("filePath").toString()));
            ImageIO.write(bufferImg, "png", byteArrayOut);
            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short) 2, 9, (short) 5, 38);
            anchor.setAnchorType(3);
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        } else {
            style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellStyle(style);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text.toString());
            }
            if (result != null) {
                int index = 1;
                for (List<String> m : result) {
                    row = sheet.createRow(index);
                    int cellIndex = 0;
                    for (String str : m) {
                        HSSFCell cell = row.createCell(cellIndex);
                        cell.setCellValue(str == null ? "" : str);
                        cellIndex++;
                    }
                    index++;
                }
            }

        }
    }

    public void exportRExcel(HSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<List<String>> result,
                               Map<String, Object> searchParams, OutputStream out) throws Exception {
        HSSFSheet sheet = workbook.createSheet();

        workbook.setSheetName(sheetNum, sheetTitle);
        sheet.setDefaultColumnWidth(30);
        sheet.setColumnWidth(0, 18*256);
        sheet.setColumnWidth(1, 35*256);

        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);


        HSSFFont font = workbook.createFont();
        font.setColor(HSSFColor.BLACK.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

        style.setFont(font);

        style.setWrapText(true);

        HSSFCellStyle headStyle = workbook.createCellStyle();
        headStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

        if (sheetNum == 0) {
            for (int i = 0; i < headers.length; i++) {
                HSSFRow row1 = sheet.createRow(i);
                HSSFCell cell = row1.createCell(0);

                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text.toString());
            }
            if (result != null) {
                int index = 0;
                int index1 = 0;
                int i = 0;
                for (List<String> m : result) {
                    if (i >= 1) {
                        index++;
                        int cellIndex = 0;
                        int cellIndex1 = 2;
                        String rValue = "";
                        String rDetail = "";
                        boolean isRed = false;
                        if (m != null && m.size() == 4) {
                            if (m.get(0).equals("R1")) {
                                if (m.get(2).equals("0")) {
                                    rValue = "[" + m.get(0) + "]";
                                } else
                                    rValue = "[" + m.get(0) + "-" + m.get(2) +  "]";

                                if (m.get(1).equals("true")) {
                                    isRed = true;
                                }
                            } else {
                                rValue = "[" + m.get(0) + "]";
                                if (m.get(1).equals("true")) {
                                    isRed = true;
                                }
                            }
                            rDetail = m.get(0) + "--" + m.get(3);
                        }
                        HSSFCellStyle contentStyle = workbook.createCellStyle();
                        HSSFFont contentFont = workbook.createFont();
                        contentFont.setColor(isRed == true ? HSSFColor.RED.index : HSSFColor.BLACK.index);
                        contentFont.setFontHeightInPoints((short) 12);
                        contentFont.setBoldweight(isRed == true ? HSSFFont.BOLDWEIGHT_BOLD : HSSFFont.BOLDWEIGHT_NORMAL);
                        contentStyle.setFont(contentFont);
                        HSSFRow row = sheet.getRow(index);
                        if (row == null) {
                            row = sheet.createRow(index);
                        }
                        HSSFCell cell1 = row.createCell(cellIndex);
                        cell1.setCellStyle(contentStyle);
                        cell1.setCellValue(rValue);


                        HSSFRow row1 = sheet.getRow(index1);
                        if (row1 == null) {
                            row1 = sheet.createRow(index1);
                        }
                        HSSFCell cell12 = row1.createCell(cellIndex1);
                        cell12.setCellValue(rDetail);
                        index1++;
                    } else {
                        int cellIndex = 1;
                        for (String str : m) {
                            HSSFRow row = sheet.getRow(index);
                            HSSFCell cell1 = row.createCell(cellIndex);
                            cell1.setCellValue(str == null ? "" : str);
                            index++;
                        }
                    }
                    i++;
                }
            }

            ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();

            BufferedImage bufferImg = ImageIO.read(new File(searchParams.get("filePath").toString()));
            ImageIO.write(bufferImg, "png", byteArrayOut);

            HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
            HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 255,(short) 2, 9, (short) 5, 38);
            anchor.setAnchorType(3);
            patriarch.createPicture(anchor, workbook.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
        } else {
            style.setFillForegroundColor(HSSFColor.PALE_BLUE.index);
            HSSFRow row = sheet.createRow(0);
            for (int i = 0; i < headers.length; i++) {
                HSSFCell cell = row.createCell(i);
                cell.setCellStyle(style);
                HSSFRichTextString text = new HSSFRichTextString(headers[i]);
                cell.setCellValue(text.toString());
            }
            if (result != null) {
                int index = 1;
                for (List<String> m : result) {
                    row = sheet.createRow(index);
                    int cellIndex = 0;
                    for (String str : m) {
                        HSSFCell cell = row.createCell(cellIndex);
                        cell.setCellValue(str == null ? "" : str);
                        cellIndex++;
                    }
                    index++;
                }
            }

        }
    }

    private short setColor(String color) {
        if (color.equals(LevelType.EXCELLENT.toString())) {
            return HSSFColor.GREEN.index;
        } else if (color.equals(LevelType.GOOD.toString())) {
            return HSSFColor.BLUE.index;

        } else if (color.equals(LevelType.ACCEPTABLE.toString())) {
            return HSSFColor.ORANGE.index;

        } else if (color.equals(LevelType.RECTIFICATION.toString())) {
            return HSSFColor.RED.index;
        }
        return HSSFColor.BLACK.index;
    }
}
